package cn.auction.biz.impl;

import cn.auction.biz.AuctionService;
import cn.auction.dao.AuctionDao;
import cn.auction.dao.AuctionUserDao;
import cn.auction.dao.impl.AuctionDaoImpl;
import cn.auction.dao.impl.AuctionUserDaoImpl;
import cn.auction.entity.Auction;
import cn.auction.entity.AuctionEnd;
import cn.auction.entity.AuctionRecord;
import cn.auction.entity.AuctionUser;
import cn.auction.util.Page;
import my.framework.dao.DatabaseUtil;

import java.util.*;

public class AuctionServiceImpl implements AuctionService {
    private AuctionDao dao = new AuctionDaoImpl();
    private AuctionUserDao userDao = new AuctionUserDaoImpl();

    public AuctionServiceImpl() {
    }

    public void add(Auction auction) throws Exception {
        try {
            this.dao.insert(auction);
            DatabaseUtil.getConnection().commit();
        } catch (Exception e) {
            e.printStackTrace();
            DatabaseUtil.getConnection().rollback();
        } finally {
            DatabaseUtil.closeConnection();
        }

    }

    public void bid(AuctionRecord record) throws Exception {
        try {
            Auction auction = this.dao.selectAuctionById(record.getAuctionId());
            if (auction.getAuctionEndTime().before(record.getAuctionTime())) {
                throw new Exception("拍卖时间结束了");
            }

            if (record.getAuctionPrice() <= auction.getAuctionStartPrice()) {
                throw new Exception("竞拍价须高于起拍价");
            }

            List<AuctionRecord> auctionRecords = this.dao.selectAuctionRecordByAuctionId(record.getAuctionId());
            if (auctionRecords != null && auctionRecords.size() > 0 && record.getAuctionPrice() <= ((AuctionRecord)auctionRecords.get(0)).getAuctionPrice()) {
                throw new Exception("竞拍价须高于最高竞拍价");
            }

            try {
                this.dao.insert(record);
                DatabaseUtil.getConnection().commit();
            } catch (Exception e) {
                e.printStackTrace();
                DatabaseUtil.getConnection().rollback();
                throw new Exception("竞拍失败，数据库错误");
            }
        } finally {
            DatabaseUtil.closeConnection();
        }

    }

    public Page<Auction> find(Auction condition, int pageIndex) throws Exception {
        Page page;
        try {
            page = this.dao.selectByPage(condition, pageIndex);
        } finally {
            DatabaseUtil.closeConnection();
        }

        return page;
    }

    public Map<String, List> find(AuctionUser user) throws Exception {
        try {
            Map<String, List> map = new HashMap();
            List<AuctionEnd> endList = new ArrayList();
            List<Auction> auctionendList = this.dao.selectAuctionByTimeOver(user);
            Map<Long, AuctionUser> users = new HashMap();
            AuctionEnd end = null;

            for(Iterator iterator = auctionendList.iterator(); iterator.hasNext(); endList.add(end)) {
                Auction auc = (Auction)iterator.next();
                end = new AuctionEnd();
                end.setAuctionDesc(auc.getAuctionDesc());
                end.setAuctionEndTime(auc.getAuctionEndTime());
                end.setAuctionId(auc.getAuctionId());
                end.setAuctionName(auc.getAuctionName());
                end.setAuctionStartPrice(auc.getAuctionStartPrice());
                end.setAuctionStartTime(auc.getAuctionStartTime());
                List<AuctionRecord> auctionRecords = this.dao.selectAuctionRecordByAuctionId(auc.getAuctionId());
                AuctionRecord endRecord = (AuctionRecord)auctionRecords.get(0);
                end.setAuctionPrice(endRecord.getAuctionPrice());
                AuctionUser endUser = null;
                if ((endUser = (AuctionUser)users.get(endRecord.getUserId())) == null) {
                    end.setUser(this.userDao.findById(endRecord.getUserId()));
                    users.put(endRecord.getUserId(), end.getUser());
                } else {
                    end.setUser(endUser);
                }
            }

            map.put("end", endList);
            List<Auction> notendList = this.dao.selectAuctionByIntime(user);
            Iterator auctionIterator = notendList.iterator();

            label75:
            while(true) {
                if (auctionIterator.hasNext()) {
                    Auction auction = (Auction)auctionIterator.next();
                    List<AuctionRecord> auctionRecords = this.dao.selectAuctionRecordByAuctionId(auction.getAuctionId());
                    auction.setAuctionRecords(auctionRecords);
                    Iterator auctionRecordIterator= auctionRecords.iterator();

                    while(true) {
                        if (!auctionRecordIterator.hasNext()) {
                            continue label75;
                        }

                        AuctionRecord record = (AuctionRecord)auctionRecordIterator.next();
                        if (users.containsKey(record.getUserId())) {
                            record.setAuctionUser((AuctionUser)users.get(record.getUserId()));
                        } else {
                            record.setAuctionUser(this.userDao.findById(record.getUserId()));
                            users.put(record.getUserId(), record.getAuctionUser());
                        }
                    }
                }

                map.put("notend", notendList);
                HashMap hashMap = (HashMap) map;
                return hashMap;
            }
        } finally {
            DatabaseUtil.closeConnection();
        }
    }

    public Auction findById(Long auctionId) throws Exception {
        Auction auc;
        try {
            Auction auction = this.dao.selectAuctionById(auctionId);
            List<AuctionRecord> auctionRecords = this.dao.selectAuctionRecordByAuctionId(auctionId);
            auction.setAuctionRecords(auctionRecords);

            if (auctionRecords != null && !auctionRecords.isEmpty()) {
                Map<Long, AuctionUser> users = new HashMap();
                Iterator iterator = auctionRecords.iterator();
                while(iterator.hasNext()) {
                    AuctionRecord record = (AuctionRecord)iterator.next();
                    if (users.containsKey(record.getUserId())) {
                        record.setAuctionUser((AuctionUser)users.get(record.getUserId()));
                    } else {
                        record.setAuctionUser(this.userDao.findById(record.getUserId()));
                        users.put(record.getUserId(), record.getAuctionUser());
                    }
                }
            }

            auc = auction;
        } finally {
            DatabaseUtil.closeConnection();
        }
        return auc;
    }
    public Page<Auction> findAll() throws Exception {
        return null;
    }
    public Map<String, Object> select() throws Exception {
        return null;
    }
}
